<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item
        label="货品名称"
        prop="goodsName"
      >
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item
        label="货品种类"
        prop="goodsCate"
      >
        <el-select
          v-model="queryParams.goodsCate"
          placeholder="请选择货品种类"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="item in cateList"
            :key="item.id"
            :label="item.goodsCate"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="是否上架"
        prop="isShelf"
      >
        <el-select
          v-model="queryParams.isShelf"
          placeholder="请选择是否上架"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="item in isShelfArray"
            :key="item.id"
            :label="item.isShelf"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:product:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="序号"
        align="center"
        prop="id"
      ><template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template></el-table-column>
      <el-table-column
        label="货品名称"
        align="center"
        prop="goodsName"
      />
      <el-table-column
        label="货品种类"
        align="center"
        prop="goodsCateName"
      />
<!--      <el-table-column-->
<!--        label="货品图片"-->
<!--        align="center"-->
<!--        prop="goodsPics"-->
<!--      >-->
<!--        <template slot-scope="scope">-->
<!--          <img-->
<!--            :src="scope.row.goodsPics"-->
<!--            class="news-image"-->
<!--          />-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="货品图片" align="center" prop="goodsPics" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.goodsPics" :width="50" :height="50"/>
        </template>
      </el-table-column>


      <el-table-column
        label="货品价格"
        align="center"
        prop="goodsPrice"
      />
      <el-table-column
        label="货品数量"
        align="center"
        prop="goodsNum"
      />
      <el-table-column
        label="是否上架"
        align="center"
        prop="isShelf"
      > <template slot-scope="scope">
          <span v-if="scope.row.isShelf == '0'">否</span>
          <span v-if="scope.row.isShelf == '1'">是</span>
        </template></el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小卖部商品对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="650px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="货品名称"
          prop="goodsName"
        >
          <el-input
            v-model="form.goodsName"
            placeholder="请输入货品名称"
          />
        </el-form-item>
        <el-form-item
          label="货品种类"
          prop="goodsCate"
        >
          <el-select
            v-model="form.goodsCate"
            placeholder="请选择货品种类"
            clearable
            size="small"
            @change="handleQuery"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.goodsCate"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="货品价格"
          prop="goodsPrice"
        >
          <el-input-number :min='0' :precision='2'
            v-model="form.goodsPrice"
            placeholder="请输入货品价格"
          />
        </el-form-item>
        <el-form-item
          label="货品数量"
          prop="goodsNum"
        >
          <el-input-number :min='0'
            v-model="form.goodsNum"
            placeholder="请输入货品数量"
          />
        </el-form-item>
        <el-form-item
          label="是否上架"
          prop="isShelf"
        >
            <el-radio-group v-model="form.isShelf">
              <el-radio :label= "'0'">否</el-radio>
              <el-radio :label= "'1'">是</el-radio>
            </el-radio-group>
        </el-form-item>
<!--        <el-form-item-->
<!--          label="货品图片"-->
<!--          prop="goodsPics"-->
<!--        >-->
<!--          <div class="updateimg">-->
<!--            <el-upload-->
<!--            v-model="form.goodsPics"-->
<!--              :action="MixinUploadAllApi"-->
<!--              :multiple="false"-->
<!--              :on-success="uploadSuccess"-->
<!--              :limit="1"-->
<!--              :before-upload="beforeImgUpload"-->
<!--            >-->
<!--              <el-button-->
<!--                size="small"-->
<!--                type="primary"-->
<!--              >点击上传</el-button>-->
<!--              <div-->
<!--                slot="tip"-->
<!--                class="el-upload__tip"-->
<!--              >-->
<!--                建议上传jpg/png文件，尺寸（宽*高）750*327-->
<!--              </div>-->
<!--            </el-upload>-->
<!--            <img :src="form.goodsPics" />-->
<!--          </div>-->
<!--        </el-form-item>-->

        <el-form-item label="货品图片" prop="goodsPics">
          <image-upload v-model="form.goodsPics"/>
        </el-form-item>


        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
  exportProduct,
} from "@/api/commissaryProduct/product";
import {
  listCate,
  getCate,
  delCate,
  addCate,
  updateCate,
  exportCate,
} from "@/api/commissaryProductCate/cate";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 小卖部商品表格数据
      productList: [],
      // 小卖部商品种类表格数据
      cateList: [],
      // 小卖部商品是否上架 0 否 1 是
      isShelfArray: [
        { id: 0, isShelf: "否" },
        { id: 1, isShelf: "是" },
      ],
      MixinUploadAllApi: process.env.VUE_APP_BASE_API + "/file/upload/single",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        goodsPics: undefined,
        goodsPrice: undefined,
        goodsNum: undefined,
        isShelf: undefined,
        goodsCate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsName: [
          { required: true, message: "货品名称不能为空", trigger: "blur" },
        ],
        goodsPics: [
          { required: true, message: "货品图片不能为空", trigger: "blur" },
        ],
        goodsPrice: [
          { required: true, message: "货品价格不能为空", trigger: "blur" },
        ],
        goodsNum: [
          { required: true, message: "货品数量不能为空", trigger: "blur" },
        ],
        isShelf: [
          { required: true, message: "是否上架不能为空", trigger: "blur" },
        ],
        goodsCate: [
          { required: true, message: "货品种类不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getProductCateList();
  },
  methods: {
    /** 图片上传之前的校验  */
    beforeImgUpload(file) {
      const isLt1M = file.size / 5120 / 5120 < 1;
      if (!isLt1M) {
        this.msgWarning("上传图片大小不能超过 5MB!");
      }
      var FileExt = file.name.replace(/.+\./, "");
      if (["jpg", "jpeg", "png"].indexOf(FileExt.toLowerCase()) === -1) {
        this.msgWarning("请上传后缀名为jpg/jpeg/png的图片附件！");
        return false;
      }
      return isLt1M;
    },
    // 主体
    uploadSuccess(res) {
      console.log(res);
      this.form.goodsPics = res.data;
      console.log(this.form);
    },
    /** 查询小卖部商品种类列表 */
    getProductCateList() {
      this.loading = true;
      listCate(this.queryParams).then((response) => {
        this.cateList = response.rows;
        this.loading = false;
      });
    },
    /** 查询小卖部商品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        goodsName: undefined,
        goodsPics: undefined,
        goodsPrice: undefined,
        goodsNum: undefined,
        isShelf: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        goodsCate: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小卖部商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProduct(id).then((response) => {
        console.log(`===============`, response.data)
        let { goodsCate} = response.data
        this.form = response.data;
        this.form.goodsCate = Number(goodsCate)
        this.open = true;
        this.title = "修改小卖部商品";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log(`form:`, this.form)
        if (valid) {
          if (this.form.id != undefined) {
            updateProduct(this.form).then((response) => {
              if (response.code === "0") {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProduct(this.form).then((response) => {
              if (response.code === "0") {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
          // this.form.goodsPics = '';
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除小卖部商品编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有小卖部商品数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportProduct(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
  },
};
</script>
